/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hr.fer.zpr.nmbp.project1.presentation.util;

import hr.fer.zpr.nmbp.project1.model.Bolest;
import java.util.List;
import javax.swing.ListModel;
import javax.swing.event.EventListenerList;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;


public class MyListModel<T> implements ListModel {

    private List<T> list;
    private EventListenerList listeners = new EventListenerList();
    
    @Override
    public int getSize() {
        return (list == null) ? 0 : list.size();
    }

    @Override
    public Object getElementAt(int index) {
        return (list == null) ? null : list.get(index);
    }

    @Override
    public void addListDataListener(ListDataListener l) {
        listeners.add(ListDataListener.class, l);
    }

    @Override
    public void removeListDataListener(ListDataListener l) {
        listeners.remove(ListDataListener.class, l);
    }
    
    public void setList(List<T> list) {
        this.list = list;
        fireEvent();
    }
    
    private void fireEvent() {
        Object[] listenerList = listeners.getListenerList();
		for(int i = 0; i < listenerList.length; i += 2) {
			ListDataListener l = (ListDataListener) listenerList[i + 1];
			l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, getSize()));
		}
    }

    public List<T> getList() {
        return list;
    }

}
